home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TODO.PAK / TODOLIST.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  328 lines

  1. //---------------------------------------------------------------------
  2. //
  3. //  TODOLIST.H
  4. //
  5. //      Copyright (c) 1991, 1995 by Borland International
  6. //      All Rights Reserved.
  7. //
  8. //  Defines the following classes, used in implementing the Todo list:
  9. //
  10. //      TodoEntry   - holds the data for an entry in the Todo list.
  11. //
  12. //      TodoList    - container for holding Todo entries
  13. //
  14. //      ListBox     - wrapper around the Windows listbox, providing
  15. //                    an interface that fits with the Todo list.
  16. //
  17. //      TodoWindow  - the main window for this application.  There's
  18. //                    nothing displayed in the window except for the
  19. //                    list box.
  20. //
  21. //---------------------------------------------------------------------
  22. #if !defined(TODOLIST_H)
  23. #define TODOLIST_H
  24.  
  25. #include <winsys/wsysinc.h>
  26. #include <classlib/date.h>
  27. #include <classlib/vectimp.h>
  28. #include <classlib/objstrm.h>
  29. #include <classlib/stdtempl.h>
  30. #include <services/except.h>
  31. #include <services/cstring.h>
  32. #include <iostream.h>
  33. #include <string.h>
  34. #include <dir.h>
  35.  
  36. #include "todowin.h"
  37.  
  38. //---------------------------------------------------------------------
  39. //
  40. //  class TodoEntry
  41. //
  42. //      holds the data for an entry in the Todo list.
  43. //
  44. //---------------------------------------------------------------------
  45.  
  46. class TodoEntry
  47. {
  48.  
  49.     friend class EditBox;
  50.  
  51. public:
  52.  
  53.     TodoEntry();                // constructor.
  54.  
  55.     BOOL Modified() const;      // indicates whether the entry has
  56.                                 // been modified.  Used in determining
  57.                                 // whether the list should be saved.
  58.  
  59.     void Clear();               // marks the entry as saved.
  60.  
  61.     friend int operator == ( const TodoEntry&, const TodoEntry& );
  62.     friend int operator <  ( const TodoEntry&, const TodoEntry& );
  63.  
  64.     friend ipstream& operator >> ( ipstream& is, TodoEntry& td );
  65.     friend opstream& operator << ( opstream& os, const TodoEntry& td );
  66.  
  67.     friend ostream& operator << ( ostream& os, const TodoEntry& tde );
  68.  
  69. private:
  70.  
  71.     BOOL Dirty;                 // indicates whether this entry has
  72.                                 // been modified.
  73.  
  74.     TDate DateCreated;
  75.     TDate DateDue;
  76.     string Text;                // the note associated with this entry
  77.     int Priority;
  78.  
  79. };
  80.  
  81. //---------------------------------------------------------------------
  82. //
  83. //  class TodoList
  84. //
  85. //      container for holding Todo entries.  Currently implemented as
  86. //      a SortedArray, so we don't have to explicitly sort the entries
  87. //      when a new one is added.  The sorting is done according to the
  88. //      operator < defined for a TodoEntry, which sorts according
  89. //      to the due date.
  90. //
  91. //---------------------------------------------------------------------
  92.  
  93. class TodoList
  94. {
  95.  
  96.     friend class ListBox;
  97.  
  98. public:
  99.  
  100.     TodoList();
  101.  
  102.     void Add( const TodoEntry& );
  103.                                 // adds an entry to the Todo list.
  104.  
  105.     void Detach( unsigned idx );
  106.                                 // removes an entry from the Todo list.
  107.  
  108.     TodoEntry& operator [] ( unsigned idx );
  109.  
  110.     int IndexOf( const TodoEntry& );
  111.                                 // returns the index of the specified entry.
  112.  
  113.     BOOL Modified() const;      // indicates whether the list has
  114.                                 // been modified by adding or deleting an
  115.                                 // entry.  Used in determining
  116.                                 // whether the list should be saved.
  117.  
  118.     void MarkSaved() const;     // marks that the list has been saved.
  119.  
  120.     void Clear();               // removes all entries from the list.
  121.  
  122.     friend ipstream& operator >> ( ipstream& is, TodoList& td );
  123.     friend opstream& operator << ( opstream& os, const TodoList& td );
  124.  
  125. private:
  126.  
  127.     TSVectorImp<TodoEntry> Vect;// sorted vector that holds the entries
  128.  
  129.     BOOL Dirty;                 // indicates whether this list has been
  130.                                 // modified.
  131.  
  132. };
  133.  
  134. //---------------------------------------------------------------------
  135. //
  136. //  class ListBox
  137. //
  138. //      wrapper around the Windows listbox, providing an interface
  139. //      that fits with the Todo list.  This is used to display the
  140. //      Todo list in a window.
  141. //
  142. //---------------------------------------------------------------------
  143.  
  144. class ListBox
  145. {
  146.  
  147. public:
  148.  
  149.     ListBox();
  150.  
  151.     const ListBox& operator = ( const TodoList& );
  152.                                 // copies the entries in the TodoList
  153.                                 // into the list box.
  154.  
  155.     void Focus();               // sets focus to the list box.
  156.     void Move( const RECT& );   // moves and resizes the list box.
  157.     int Current();              // returns the index of the current
  158.                                 // selection.
  159.     void Remove( int );         // removes the specified entry from
  160.                                 // the list box.
  161.     void Insert( int, const TodoEntry& );
  162.                                 // adds an entry to the list box.
  163.     void Replace( int, const TodoEntry& );
  164.                                 // replaces an entry in the list box
  165.                                 // with another entry.
  166.     void Select( int );         // selects the specified entry.
  167.     void Clear();               // removes all entries.
  168.  
  169.     void Create( HWND, HWND, const RECT& );
  170.                                 // builds the list box.  This can't be
  171.                                 // done in the constructor because we
  172.                                 // don't have enough information at the
  173.                                 // time of construction.
  174.  
  175. private:
  176.  
  177.     HWND hListBox;              // handle of the list box.
  178.  
  179. };
  180.  
  181. //---------------------------------------------------------------------
  182. //
  183. //  class TodoWindow
  184. //
  185. //      the main window for this application.  There's nothing displayed
  186. //      in the window except for the list box.
  187. //---------------------------------------------------------------------
  188.  
  189. class TodoWindow : public Window
  190. {
  191.  
  192. public:
  193.  
  194.     TodoWindow() { *TitleName = '\0'; *FileName = '\0'; }
  195.  
  196. protected:
  197.  
  198.     virtual LONG Dispatch( WPARAM, WPARAM, LPARAM );
  199.  
  200.     virtual BOOL RegisterClass();
  201.     virtual BOOL CreateNewWindow();
  202.  
  203. private:
  204.  
  205.     ListBox LB;                 // the list box used by this window.
  206.     TodoList Tdl;               // the Todo list being displayed in this
  207.                                 // window.  There's a lot of parallelism
  208.                                 // between the operations of these two
  209.                                 // objects, and it might be worthwhile
  210.                                 // to add a class derived from both
  211.                                 // ListBox and TodoList for use here.
  212.  
  213.     char TitleName[MAXFILE+MAXEXT];
  214.     char FileName[MAXPATH];     // path to the file currently being
  215.                                 // used.  "" if there is no file.
  216.  
  217.     void NewList();
  218.     void OpenFile();
  219.     void SaveFile();
  220.     void SaveFileAs();
  221.     void ShowEditBox();
  222.     void NewEntry();
  223.     void DelEntry();
  224.     void ShowAboutBox();
  225.  
  226.     BOOL ShowSaveBox();
  227.  
  228.     void MoveListBox();
  229.  
  230.     void ReadFile();
  231.     void WriteFile();
  232.     void CheckSave();
  233.  
  234.     BOOL ProcessCommand( WPARAM, LPARAM );
  235.  
  236. };
  237.  
  238. //---------------------------------------------------------------------
  239. //
  240. //  inline functions.
  241. //
  242. //---------------------------------------------------------------------
  243.  
  244. inline TodoEntry::TodoEntry() : Dirty( FALSE ), Priority( 1 )
  245. {
  246. }
  247.  
  248. inline void TodoEntry::Clear()
  249. {
  250.     Dirty = 0;
  251. }
  252.  
  253. inline BOOL TodoEntry::Modified() const
  254. {
  255.     return Dirty;
  256. }
  257.  
  258. inline int operator == ( const TodoEntry& e1, const TodoEntry& e2 )
  259. {
  260.     return e1.DateDue == e2.DateDue;
  261. }
  262.  
  263. inline int operator <  ( const TodoEntry& e1, const TodoEntry& e2 )
  264. {
  265.     return e1.DateDue < e2.DateDue;
  266. }
  267.  
  268. inline TodoList::TodoList() : Vect( 20, 5 )
  269. {
  270. }
  271.  
  272. inline TodoEntry& TodoList::operator [] ( unsigned idx )
  273. {
  274.     return Vect[idx];
  275. }
  276.  
  277. inline ListBox::ListBox() : hListBox( 0 )
  278. {
  279. }
  280.  
  281. inline void ListBox::Focus()
  282. {
  283.     if( IsWindow( hListBox ) )
  284.         SetFocus( hListBox );
  285. }
  286.  
  287. inline void ListBox::Move( const RECT& wrect )
  288. {
  289.     MoveWindow( hListBox,
  290.                 wrect.left,
  291.                 wrect.top,
  292.                 wrect.right - wrect.left,
  293.                 wrect.bottom - wrect.top,
  294.                 TRUE
  295.               );
  296. }
  297.  
  298. inline int ListBox::Current()
  299. {
  300.     return (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  301. }
  302.  
  303. inline void ListBox::Remove( int i )
  304. {
  305.     SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  306.     Select( i );
  307. }
  308.  
  309. inline void ListBox::Replace( int i, const TodoEntry& tde )
  310. {
  311.     Remove( i );
  312.     Insert( i, tde );
  313. }
  314.  
  315. inline void ListBox::Select( int i )
  316. {
  317.     i = min( i, (int)SendMessage( hListBox, LB_GETCOUNT, 0, 0 ) - 1 );
  318.     SendMessage( hListBox, LB_SETCURSEL, i, 0 );
  319. }
  320.  
  321. inline void ListBox::Clear()
  322. {
  323.     SendMessage( hListBox, LB_RESETCONTENT, 0, 0 );
  324. }
  325.  
  326. #endif  // __TODOLIST_H
  327.  
  328.